package cn.iocoder.yudao.module.pplan.controller.admin.tableware;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.pplan.controller.admin.tableware.vo.TablewarePageReqVO;
import cn.iocoder.yudao.module.pplan.controller.admin.tableware.vo.TablewareRespVO;
import cn.iocoder.yudao.module.pplan.controller.admin.tableware.vo.TablewareSaveReqVO;
import cn.iocoder.yudao.module.pplan.dal.dataobject.tableware.TablewareDO;
import cn.iocoder.yudao.module.pplan.service.tableware.TablewareService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 生产计划 - 餐具管理")
@RestController
@RequestMapping("/pplan/tableware")
@Validated
public class TablewareController {

    @Resource
    private TablewareService tablewareService;

    @PostMapping("/create")
    @Operation(summary = "创建餐具管理")
    @PreAuthorize("@ss.hasPermission('pplan:tableware:create')")
    public CommonResult<Long> createTableware(@Valid @RequestBody TablewareSaveReqVO createReqVO) {
        return success(tablewareService.createTableware(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新餐具管理")
    @PreAuthorize("@ss.hasPermission('pplan:tableware:update')")
    public CommonResult<Boolean> updateTableware(@Valid @RequestBody TablewareSaveReqVO updateReqVO) {
        tablewareService.updateTableware(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除餐具管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('pplan:tableware:delete')")
    public CommonResult<Boolean> deleteTableware(@RequestParam("id") Long id) {
        tablewareService.deleteTableware(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除餐具管理")
                @PreAuthorize("@ss.hasPermission('pplan:tableware:delete')")
    public CommonResult<Boolean> deleteTablewareList(@RequestParam("ids") List<Long> ids) {
        tablewareService.deleteTablewareListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得餐具管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('pplan:tableware:query')")
    public CommonResult<TablewareRespVO> getTableware(@RequestParam("id") Long id) {
        TablewareDO tableware = tablewareService.getTableware(id);
        return success(BeanUtils.toBean(tableware, TablewareRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得餐具管理分页")
    @PreAuthorize("@ss.hasPermission('pplan:tableware:query')")
    public CommonResult<PageResult<TablewareRespVO>> getTablewarePage(@Valid TablewarePageReqVO pageReqVO) {
        PageResult<TablewareDO> pageResult = tablewareService.getTablewarePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, TablewareRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出餐具管理 Excel")
    @PreAuthorize("@ss.hasPermission('pplan:tableware:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTablewareExcel(@Valid TablewarePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<TablewareDO> list = tablewareService.getTablewarePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "餐具管理.xls", "数据", TablewareRespVO.class,
                        BeanUtils.toBean(list, TablewareRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得工具精简列表", description = "主要用于前端的下拉选项")
    public CommonResult<List<TablewareRespVO>> getSimpleTablewareist(@RequestParam("name") String name) {
        TablewarePageReqVO reqVO = new TablewarePageReqVO();
        reqVO.setName(name);
        reqVO.setPageSize(PAGE_SIZE_NONE); // 不分页
        List<TablewareDO> list = tablewareService.getTablewarePage(reqVO).getList();
        // 拼接 VO 返回，只返回 id、name、key
        return success(convertList(list, tableware -> new TablewareRespVO()
                .setId(tableware.getId()).setName(tableware.getName())));
    }

}